<?php

namespace haibao\cms\business;

class BrandImg extends \haibao\cms\business\Base{
	/**
	 * 添加品牌大图
	 * @param \haibao\cms\model\data\brandImg $brandImgModel;
	 */
	public static function addBrandImg($brandImgModel){
		self::checkOperation(self::FUNC_CODE_BRAND_IMAGE_ADD);
		
		if(!$brandImgModel || !($brandImgModel instanceof  \haibao\cms\model\data\BrandImg)){
			throw new BusinessException('品牌图片不存在',self::DATA_EMPTY);
		}
		if(!$brandImgModel->ImgId){
			throw new BusinessException('图片Id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		if(!$brandImgModel->BrandId){
			throw new BusinessException('品牌Id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		$brandImgModel->CreateBy = self::getCurrentUser()->Id;
		$brandImgModel->CreateTime = new \DateTime();
		$brandImgModel->Status = \haibao\cms\model\data\BrandImg::STATUS_ENABLE;
		
		$imageData = new \haibao\cms\data\ImageLibrary();
		$imageData->updateImageStatus($brandImgModel->ImgId,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,self::getCurrentUser()->UserName);
		$brandImgData = new \haibao\cms\data\BrandImg();
		$id = $brandImgData->add($brandImgModel);
		
		self::saveOperateHistory(self::FUNC_CODE_BRAND_IMAGE_ADD, $brandImgModel->toArray());
		return $id;
	}
	/**
	 * @param \haibao\cms\model\filter\BrandImg $filter
	 */
	public static function getAll($filter){
		$brandImgData = new \haibao\cms\data\BrandImg();
		return $brandImgData->getAll($filter);
	}
	/**
	 * 通过ID 查找
	 */
	public static function getBrandImgById($id){
		$brandImgData = new \haibao\cms\data\BrandImg();
		return $brandImgData->getOneById($id);
	}
	/**
	 * 删除品牌图片
	 */
	public static function deleteBrandImg($id,$status = \haibao\cms\model\data\ImageLibrary::STATUS_DISABLE){
		self::checkOperation(self::FUNC_CODE_BRAND_IMAGE_DELETE);
		if(!$id){
			throw new BusinessException('图片Id不能为空',self::DATA_EMPTY_ID);
		}
		
		$imageData = new \haibao\cms\data\ImageLibrary();
		$imgInfo = self::getBrandImgById($id);
		$imageData->updateImageStatus($imgInfo->ImgId,$status,self::getCurrentUser()->UserName);
		
		$brandImgData = new \haibao\cms\data\BrandImg();
		$brandImgData->deleteBrandImg($id,$status);
	
		self::saveOperateHistory(self::FUNC_CODE_BRAND_IMAGE_DELETE, array('id'=>$id));
	}
}